﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;

namespace WebUI.BL
{
    public class ProductColorBL
    {
        public void Insert(tbl_ProductColor productColor)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_ProductColor newproductColor = new tbl_ProductColor()
                    {
                        Status=productColor.Status,
                        Img=productColor.Img,
                        CreateUser=productColor.CreateUser,
                        CreatedDate=productColor.CreatedDate,
                        ColorName=productColor.ColorName,
                        ColorDesc=productColor.ColorDesc,
                        ColorCode=productColor.ColorCode
                    };
                    contextdb.tbl_ProductColors.InsertOnSubmit(newproductColor);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_ProductColor productColor)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateproductColor = contextdb.tbl_ProductColors.SingleOrDefault(u => u.ColorNo == productColor.ColorNo);
                try
                {
                    if (updateproductColor != null)
                    {
                        updateproductColor.Status=productColor.Status;
                        updateproductColor.Img=productColor.Img;
                        updateproductColor.CreateUser=productColor.CreateUser;
                        updateproductColor.CreatedDate=productColor.CreatedDate;
                        updateproductColor.ColorName=productColor.ColorName;
                        updateproductColor.ColorDesc=productColor.ColorDesc;
                        updateproductColor.ColorCode = productColor.ColorCode;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int productColorNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproductColor = contextdb.tbl_ProductColors.SingleOrDefault(u => u.ColorNo == productColorNo);
                try
                {
                    if (deleteproductColor != null)
                    {
                        contextdb.tbl_ProductColors.DeleteOnSubmit(deleteproductColor);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void DeleteByStatus(int productColorNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproductColor = contextdb.tbl_ProductColors.SingleOrDefault(u => u.ColorNo == productColorNo);
                try
                {
                    if (deleteproductColor != null)
                    {
                        deleteproductColor.Status = "Deleted";
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
    }
}
